package api

import (
	"admin100/app/model"
	"admin100/app/service"
	"admin100/library/define"
	"admin100/library/define/defErr"
	"admin100/library/response"

	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/util/gconv"
)

var MsgTemplateMgrApi = msgTemplateMgr{}

type msgTemplateMgr struct{}

// @summary 后台信息模板列表
// @tags 后台工具
// @produce json
// @param   page query int true "当前页数"
// @param   pageSize query int true "每页数据数量"
// @router  /msg-template/list [GET]
// @success 200 {object} response.JsonResponse "执行结果" {object} common.PageInfo "分页信息"
func (a *msgTemplateMgr) List(r *ghttp.Request) {
	var (
		req         model.MsgTemplateMgrApiListReq
		serviceData model.MsgTemplateMgrServiceListReq
	)
	if err := r.ParseForm(&req); err != nil {
		response.JsonExitWithMsg(r, define.Fail, err.Error())
	}

	if err := gconv.Struct(req, &serviceData); err != nil {
		defErr.Panic(define.ReqParamsIllegal)
	}

	total, data := service.MsgTemplateService.List(&serviceData)

	response.JsonSuc(r, map[string]interface{}{
		"total": total,
		"rows":  data,
	})

}

// @summary 后台信息模板-添加
// @tags 后台工具
// @produce json
// @param   name query string true "名称"
// @param   state query int true "启用状态：0禁用，1启用"
// @param   phoneMsgCode query []int true "短信code"
// @param   mailUserList query []int true "短信code"
// @router  /msg-template/add [POST]
// @success 200 {object} response.JsonResponse "执行结果"
func (a *msgTemplateMgr) Add(r *ghttp.Request) {

	u := service.Context.GetUser(r.Context())
	var (
		req         model.MsgTemplateMgrApiAddReq
		serviceData model.MsgTemplateMgrServicesAddReq
	)
	serviceData.UserId = u.Id

	if err := r.ParseForm(&req); err != nil {
		response.JsonExitWithMsg(r, define.Fail, err.Error())
	}

	if err := gconv.Struct(req, &serviceData); err != nil {
		defErr.Panic(define.ReqParamsIllegal)
	}
	res := service.MsgTemplateService.Add(&serviceData)

	response.JsonSuc(r, res)
}

// @summary 后台信息模板-修改
// @tags 后台工具
// @produce json
// @param   id query int true "id"
// @param   name query string false "名称"
// @param   state query int false "启用状态：0禁用，1启用"
// @param   phoneMsgCode query []int false "短信code"
// @param   mailUserList query []int false "短信code"
// @router  /msg-template/edit [POST]
// @success 200 {object} response.JsonResponse "执行结果"
func (a *msgTemplateMgr) Edit(r *ghttp.Request) {
	u := service.Context.GetUser(r.Context())
	var (
		req         model.MsgTemplateMgrApiEditReq
		serviceData model.MsgTemplateMgrServicesEditReq
	)
	serviceData.UserId = u.Id

	if err := r.ParseForm(&req); err != nil {
		response.JsonExitWithMsg(r, define.Fail, err.Error())
	}

	if err := gconv.Struct(req, &serviceData); err != nil {
		defErr.Panic(define.ReqParamsIllegal)
	}
	res := service.MsgTemplateService.Edit(&serviceData)

	response.JsonSuc(r, res)
}

// @summary 后台信息模板-删除
// @tags 后台工具
// @produce json
// @param   ids query []int true "id"
// @router  /msg-template/del [POST]
// @success 200 {object} response.JsonResponse "执行结果"
func (a *msgTemplateMgr) Del(r *ghttp.Request) {
	var (
		req         model.MsgTemplateMgrApiDelReq
		serviceData model.MsgTemplateMgrServicesDelReq
	)

	if err := r.ParseForm(&req); err != nil {
		response.JsonExitWithMsg(r, define.Fail, err.Error())
	}

	if err := gconv.Struct(req, &serviceData); err != nil {
		defErr.Panic(define.ReqParamsIllegal)
	}
	res := service.MsgTemplateService.Del(&serviceData)

	response.JsonSuc(r, res)
}
